﻿namespace Modbus.Net8.Commom
{
    /// <summary>
    /// https://blog.csdn.net/ljklxlj/article/details/141139566?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-0-141139566-blog-132941539.235^v43^pc_blog_bottom_relevance_base9&spm=1001.2101.3001.4242.1&utm_relevant_index=3
    /// </summary>
    public class LRC
    {
        public static byte ComputeCheckSum(byte[] data)
        {
            //int sum = data.Sum(x => x);
            //byte lrc = (byte)(sum & 0xff);
            //lrc = (byte)(~lrc + 1);
            //return lrc;

            int sum = data.Sum(x => x);
            return (byte)((~sum + 1) & 0xff);
        }

        public static byte ComputeCheckSum(byte[] data, int len, int idx = 0)
        {
            int sum = data.Skip(idx).Take(len).Sum(x => x);
            return (byte)((~sum + 1) & 0xff);
        }

    }
}
